package projectdemo02;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:咭咭熊
 * Date:2021 - 10 - 15
 * Time:15:50
 */
//字牌游戏
 class Card{
     public String suit;
     public int rank;

    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }

    @Override
    public String toString() {
        /*return "Card{" +
                "suit='" + suit + '\'' +
                ", rank=" + rank +
                '}';*/
        return "[" + suit + "," +rank + "]";
    }
}
class DeckCard{
    //定义牌的花色
    public static final String[] suits= {"♥","♠","♦","♣"};

    List<Card> cardList = new ArrayList<>();

    /***
     * 1.得到整符牌
     * @return
     */
     public List<Card> buyCard(){

         for (int i = 0; i < 4; i++) {
             for (int j = 0; j <= 13 ; j++) {
                String suit = suits[i];
                int rank = j;
                 Card card = new Card(suit,rank);//构造一张牌：包含牌的花色和大小
                 cardList.add(card);
             }
         }
         return  cardList;
    }

    /***
     * 2.洗牌
     * 生成随机数对数组元素进行交换
     * @param cardList
     */
    public void flushCard(List<Card> cardList){

        for (int i = cardList.size() - 1; i > 0 ; i--) {
            Random random = new Random();
            int rand = random.nextInt(i);
            swap(cardList,i,rand);
        }
    }

    /**
     * 对整幅牌进行交换
     * @param cardList
     * @param i
     * @param j
     */
    private void swap(List<Card> cardList, int i, int j) {
        Card tmp = cardList.get(i);
        cardList.set(i,cardList.get(j));
        cardList.set(j,tmp);

    }

}
public class TestDemo {
    public static void main(String[] args) {
        DeckCard deckCard = new DeckCard();
        //1.得到整幅牌
        System.out.println("============得到整幅牌==========");
        List<Card> cardList = deckCard.buyCard();
        System.out.println(cardList);
        //2.洗牌
        System.out.println("===========洗牌================");
        deckCard.flushCard(cardList);
        System.out.println(cardList);
        System.out.println("牌的总数量" + cardList.size());

        //3.发牌
        System.out.println("===========发牌================");

        //3个人，每个人5张牌:轮流拿牌
        List<Card> person1 = new ArrayList<>();
        List<Card> person2 = new ArrayList<>();
        List<Card> person3 = new ArrayList<>();
        // List<List<Card>>类似二维数组
        List<List<Card>> persons = new ArrayList<>();
        persons.add(person1);
        persons.add(person2);
        persons.add(person3);

        //每个人先拿5张牌
/*        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 5; j++) {
                //拿牌
                Card card = cardList.remove(0);
                persons.get(i).add(card);
            }
        }*/

        //实现轮流拿牌
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //拿牌
                Card card = cardList.remove(0);
                persons.get(j).add(card);
            }
        }
        System.out.println("第一个人：");
        System.out.println(person1);
        System.out.println("第二个人：");
        System.out.println(person2);
        System.out.println("第三个人：");
        System.out.println(person3);

        System.out.println("=============查看剩余的牌================");
        System.out.println(cardList);
        System.out.println("剩余牌的数量" + cardList.size());

    }
}


